//
//  GXContentMessageFooterView.m
//  YIChuSeller
//
//  Created by 田小椿 on 2019/1/3.
//  Copyright © 2019年 xiangshengjun. All rights reserved.
//

#import "GXContentMessageFooterView.h"
#import "OrderAndUploadPhotoMananger.h"
@interface GXContentMessageFooterView ()
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;
@property (weak, nonatomic) IBOutlet UIButton *refuseBtn;
@property (weak, nonatomic) IBOutlet UIButton *acceptBtn;

@end
@implementation GXContentMessageFooterView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib
{
    [super awakeFromNib];
    self.refuseBtn.enabled = NO;
    self.acceptBtn.enabled = NO;
}

+(instancetype)messageFooterView
{
    return [[NSBundle mainBundle]loadNibNamed:@"GXContentMessageFooterView" owner:nil options:nil].lastObject;
}

- (void)setFootDict:(NSDictionary *)footDict
{
    _footDict = footDict;
    self.totalPriceLabel.text = [NSString stringWithFormat:@"¥%@",footDict[@"total"]];
    if ([footDict[@"state"] isEqualToString:@"0"]) {
        self.refuseBtn.enabled = YES;
        self.acceptBtn.enabled = YES;
    }
    if ([footDict[@"state"] isEqualToString:@"1"]) {
        self.refuseBtn.enabled = YES;
        [self.refuseBtn setTitle:@"联系用户" forState:UIControlStateNormal];
        self.acceptBtn.hidden = YES;
    }
    if ([footDict[@"state"] isEqualToString:@"2"]||[footDict[@"state"] isEqualToString:@"3"]) {
        self.refuseBtn.hidden = YES;
        self.acceptBtn.hidden = YES;
    }
}

- (IBAction)pressRefuseBtn:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"联系用户"]) {
        [self contactUserWithPhoneNumber];

    }else
    {
        [self changeOrdState:@"3"];
    }
}
- (IBAction)pressAcceptBtn:(UIButton *)sender {
     [self changeOrdState:@"1"];
}
- (void)contactUserWithPhoneNumber
{
    NSString *message = [NSString stringWithFormat:@"客户号码:%@",self.footDict[@"phone"]];
    [UIAlertController showAlertInViewController:self.getViewController withTitle:nil message:message cancelButtonTitle:@"取消" destructiveButtonTitle:@"联系客户" otherButtonTitles:nil tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
        if (buttonIndex ==1) {
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",self.footDict[@"phone"]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    }];
}
- (void)changeOrdState:(NSString *)state
{
    NSString *mess;
    if ([state isEqualToString:@"3"]) {
        mess = @"确认要拒绝该订单吗？";
    }
    else if([state isEqualToString:@"1"]){
        mess = @"确认接受该订单吗？";
    }
    NSString *oNumberStr = [NSString stringWithFormat:@"%@",_footDict[@"oNumber"]];
    [UIAlertController showAlertInViewController:self.getViewController withTitle:nil message:mess cancelButtonTitle:@"取消" destructiveButtonTitle:@"确认" otherButtonTitles:nil tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
        if (buttonIndex==1) {
            [OrderAndUploadPhotoMananger ChangeShopOrderState:state oNumber:oNumberStr success:^(id responseObject) {
                if (responseObject) {
                    if ([self.delegate respondsToSelector:@selector(pressButtonChangeOrderState:)]) {
                        [self.delegate pressButtonChangeOrderState:state];
                    }
                }
            } fail:^(id responseObject) {
            }];
        }
    }];
}


@end
